 <!--
 * @Author: zeng_ze
 * @Date: 2020-1-2
 * @Description: 系统管理 -> 定时任务 -> 执行器管理
 -->
<template>
  <div>
    <fks-query-page
      v-loading="loading"
      :data="tableData"
      :page="false"
      :element-loading-text="LANG.LOADING"
      :table-name="LANG.ACTUATORMANAGEMENT_ACTUATORLIST"
    >
      <template slot="search" />
      <template slot="button">
        <fks-button
          type="primary"
          icon="fks-icon-plus"
          @click="add"
        >
          {{ LANG.ADD }}
        </fks-button>
      </template>
      <template>
        <fks-table-column
          type="index"
          align="center"
        >
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </fks-table-column>
        <fks-table-column
          prop="appName"
          :label="LANG.APPNAME"
          min-width="200"
          align="left"
        />
        <fks-table-column
          prop="title"
          :label="LANG.NAME"
          min-width="130"
        />
        <fks-table-column
          prop="addressType"
          :label="LANG.ACTUATORMANAGEMENT_REGISTER"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.addressType == 1">{{
              LANG.ACTUATORMANAGEMENT_MANUALLY
            }}</span>
            <span v-if="scope.row.addressType == 0">{{
              LANG.ACTUATORMANAGEMENT_AUTOMATIC
            }}</span>
          </template>
        </fks-table-column>
        <fks-table-column
          :label="LANG.OPERATION"
          align="center"
          width="100"
        >
          <template slot-scope="scope">
            <fks-button
              type="text"
              @click="editHandle(scope.row)"
            >
              {{ LANG.EDIT }}
            </fks-button>
            <fks-button
              danger-text
              @click="deleteHandle(scope.row)"
            >
              {{ LANG.DELETE }}
            </fks-button>
          </template>
        </fks-table-column>
      </template>
    </fks-query-page>
    <!-- 新增&&编辑执行器 同一个表单 -->
    <fks-dialog
      :visible.sync="addEditVisible"
      :append-to-body="true"
      width="960px"
      destroy-on-close
      :title="title"
    >
      <div slot="title">
        <i
          v-if="title == LANG.ADD"
          class="fks-icon-plus"
        />
        <i
          v-if="title == LANG.EDIT"
          class="fks-icon-edit"
        />
        {{ title }}
      </div>
      <fks-form
        ref="Form"
        :model="Form"
        :rules="rules"
        status-icon
      >
        <fks-form-item
          :span="12"
          lead
          :label="LANG.APPNAME"
          prop="appName"
        >
          <fks-input
            v-model="Form.appName"
            maxlength="50"
          />
        </fks-form-item>
        <fks-form-item
          :span="12"
          :label="LANG.NAME"
          prop="title"
        >
          <fks-input
            v-model="Form.title"
            maxlength="50"
          />
        </fks-form-item>
        <fks-form-item
          :span="12"
          lead
          :label="LANG.SORT"
          prop="orderParam"
        >
          <fks-input
            v-model="Form.orderParam"
            maxlength="9"
          />
        </fks-form-item>
        <fks-form-item
          :span="12"
          :label="LANG.ACTUATORMANAGEMENT_REGISTER"
          prop="addressType"
        >
          <fks-input
            :placeholder="LANG.ACTUATORMANAGEMENT_AUTOMATIC"
            disabled
          />
        </fks-form-item>
        <div style="text-align: right">
          <fks-button
            type="primary"
            class="fks-icon-check"
            @click="save"
          >
            {{ LANG.SAVE }}
          </fks-button>
        </div>
      </fks-form>
    </fks-dialog>
  </div>
</template>
 
<script>
import * as Timedtask from '../api'
export default {
  name: 'ActuatorManagement',
  components: {},
  data() {
    return {
      currentPage: 1,
      total: 0,
      pageSize: 20,
      loading: false,
      //新增&编辑执行器
      title: '',
      addEditVisible: false,
      tableData: [],
      Form: {
        appName: '',
        title: '',
        order: '',
        //注册方式 默认手动注册
        addressType: 0
      }
    }
  },
  computed: {
    //验证规则
    rules() {
      return {
        appName: [
          {
            required: true,
            message: this.LANG.ACTUATORMANAGEMENT_NOT_EMPTY,
            trigger: ['blur']
          }
        ],
        title: [
          {
            required: true,
            message: this.LANG.ACTUATORMANAGEMENT_NOT_EMPTY,
            trigger: ['blur']
          }
        ],
        orderParam: [
          {
            required: true,
            message: this.LANG.ACTUATORMANAGEMENT_NOT_EMPTY,
            trigger: ['blur']
          }
        ]
      }
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    //查看任务执行器主页数据
    getData() {
      Timedtask.getExecutor()
        .then((res) => {
          if (res.status) {
            this.tableData = res.data
          }
          this.loading = false
        })
        .catch(() => {
          this.loading = false
          this.tableData = []
        })
    },
    //新增&编辑执行器
    save() {
      this.Form.addressType = 0
      if (this.title == this.LANG.ADD) {
        this.$refs.Form.validate((valid) => {
          if (valid) {
            Timedtask.addExecutor(JSON.stringify(this.Form))
              .then((res) => {
                if (res.status) {
                  this.$message.success(this.LANG.SUCCESS)
                  this.addEditVisible = false
                  this.getData()
                }
              })
              .catch(() => {})
          } else {
            this.$message.error(this.LANG.ACTUATORMANAGEMENT_ERROR)
          }
        })
      } else {
        this.$refs.Form.validate((valid) => {
          if (valid) {
            Timedtask.editExecutor(JSON.stringify(this.Form))
              .then((res) => {
                if (res.status) {
                  this.$message.success(this.LANG.SUCCESS)
                  this.addEditVisible = false
                  this.getData()
                }
              })
              .catch(() => {})
          } else {
            this.$message.error(this.LANG.ACTUATORMANAGEMENT_ERROR)
          }
        })
      }
    },
    //新增执行器
    add() {
      this.title = this.LANG.ADD
      this.Form = {}
      this.addEditVisible = true
    },
    //编辑执行器
    editHandle(row) {
      this.title = this.LANG.EDIT
      this.Form = JSON.parse(JSON.stringify(row))
      this.addEditVisible = true
    },
    cancel() {
      this.addEditVisible = false
    },
    //删除执行器
    deleteHandle(row) {
      this.$confirm(this.LANG.DELETE + '"' + row.title + '"？', {
        title: this.LANG.TIPS,
        type: 'warning',
        confirmButtonText: this.LANG.CONFIRM,
        cancelButtonText: this.LANG.CANCEL,
        confirmButtonClass: 'fks-button--danger',
        deleteMode: true
      })
        .then(() => {
          Timedtask.deleteExecutor(row.id)
            .then((res) => {
              if (res.status) {
                this.$message.success(this.LANG.SUCCESS)
                this.getData()
              }
            })
            .catch(() => {})
        })
        .catch(() => {})
    }
  }
}
</script>
<style lang="scss" scoped>
</style>